package com.example.shixun.controller;

import com.example.shixun.pojo.Result;
import com.example.shixun.pojo.School;
import com.example.shixun.pojo.SchoolUser;
import com.example.shixun.pojo.User;
import com.example.shixun.service.SchoolService;
import com.example.shixun.service.SchoolUserService;
import com.example.shixun.service.UserService;
import com.example.shixun.utils.PageInfoUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author XDW
 * @Version 1.0
 * @date 2023/6/23 14:16
 */

@Controller
@ResponseBody
@RequestMapping("/school")
public class SchoolController {
    @Resource
    SchoolService schoolService;
    @Resource
    UserService userService;
    @Resource
    SchoolUserService schoolUserService;
    @GetMapping("/list")
    public Result<PageInfoUtil<School>> getSchoolList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                      @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                      School school) {
//        System.out.println(school.toString());
        PageInfoUtil<School> pageInfoUtil = new PageInfoUtil<>();
        pageInfoUtil.setPageNum(pageNum);
        pageInfoUtil.setPageSize(pageSize);
        pageInfoUtil.setParams(school);
        PageInfoUtil<School> pageInfo = schoolService.selectAll(pageInfoUtil);
        return Result.success(pageInfo);
    }

    @GetMapping("/search")
    public Result<School> searchSchool(@RequestParam(value = "name", required = false) String name, @RequestParam(value = "id", required = false) Integer id) {
        School school;
        if (name != null) {
            school = schoolService.select(name);
        } else if (id != null) {
            school = schoolService.select(id);
        } else {
            return Result.fail("参数错误");
        }

        if (school != null) {
            return Result.success(school);
        } else {
            return Result.fail("学校不存在");
        }
    }

    @PostMapping("/add")
    public Result<Integer> addSchool(@RequestBody School school) {
        int result = schoolService.saveSchool(school);
        if (result > 0) {
            return Result.success(result);
        } else {
            return Result.fail("添加学校失败");
        }
    }

    @PostMapping("/update")
    public Result<Integer> updateSchool(@RequestBody School school) {
        int result = schoolService.updateSchool(school);
        if (result > 0) {
            return Result.success(result);
        } else {
            return Result.fail("更新学校信息失败");
        }
    }

    @GetMapping("/delete")
    public Result<Integer> deleteSchoolById(@RequestParam("id") int id) {
        int result = schoolService.deleteSchool(id);
        if (result > 0) {
            return Result.success(result);
        } else {
            return Result.fail("删除学校失败");
        }
    }
    @GetMapping("/cancelDelete")
    public Result<Integer> cancelDeleteById(@RequestParam("id") Integer id){
        int n=schoolService.cancelDelete(id);
        if(n>0) return Result.success("恢复成功",n);
        return Result.fail("恢复失败");
    }
    @GetMapping("/deleteByName")
    public Result<Integer> deleteSchoolByName(@RequestParam("name") String name) {
        int result = schoolService.deleteSchool(name);
        if (result > 0) {
            return Result.success(result);
        } else {
            return Result.fail("删除学校失败");
        }
    }
    @ApiOperation("添加老师")
    @PostMapping("/addTeacher")
    public Result addTeacher(@RequestBody SchoolUser schoolUser){
        if(schoolUserService.findByUserId(schoolUser.getUserId())!=null) return Result.fail("该老师已有所属学校");
        Integer n = schoolUserService.save(schoolUser);
        User user=new User();
        user.setSchoolName(schoolService.select(schoolUser.getSchoolId()).getName());
        userService.updateUser(user);
        return Result.success(n);
    }
    @ApiOperation("根据老师id查找学校")
    @GetMapping("/findByTeacherId")
    public Result findBySchoolId(Integer id){
        List<SchoolUser> schoolIdList = schoolUserService.findBySchoolId(id);
        List<School> schoolList=new ArrayList<>();
        for(SchoolUser list:schoolIdList){
            Integer uid= list.getUserId();
            schoolList.add(schoolService.select(uid));
        }
        return Result.success(schoolList);
    }
}
